/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.app.gui;
import edu.app.dao.RecetteDAO;
import edu.app.dao.IDAO;
import edu.app.persistence.Recette;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jdesktop.observablecollections.ObservableCollections;

/**
 *
 * @author Karim
 */

public class Form_Modifier extends javax.swing.JFrame {
    
   private int fav;
    private IDAO<Recette> recetteDAO= new RecetteDAO();
    private String categorie;
    /**
     * Creates new form RecetteMasterDetailsForm
     */
    public Form_Modifier(String nom, String categorie2, String ingredient2, int duree, float note2, int favorie2, String remarques2, String image, float cout2, int nbr_personne2, String origine2) {
        initComponents();
           setResizable(false);
        setBounds(50, 50, 1200, 830);
        getContentPane().setLayout(null);
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();
        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2)-20;
        this.setLocation(windowX, windowY);

        titre.setText(nom);
        categorie= categorie2;
        ingredients.setText(ingredient2);
        dureeField.setText(Integer.toString(duree));
        note.setText(Float.toString(note2));
        if (favorie2==1){
           jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/like.jpg")));
        }
        else {
            jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/likeoff.jpg")));
        }

        remarquesField.setText(remarques2);
        cout.setText(Float.toString(cout2));
        nbr_personnes.setText(Integer.toString(nbr_personne2));
        origine.setText(origine2);
        fav=favorie2;
      
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        recettes = ObservableCollections.observableList(recetteDAO.findAll(""));
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ingredients = new javax.swing.JTextPane();
        dureeField = new javax.swing.JTextField();
        note = new javax.swing.JTextField();
        remarquesField = new javax.swing.JTextField();
        origine = new javax.swing.JTextField();
        cout = new javax.swing.JTextField();
        nbr_personnes = new javax.swing.JTextField();
        path = new javax.swing.JTextField();
        browsbout = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        titre = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setTitle("Modifier un recette");
        getContentPane().setLayout(null);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/home.jpg"))); // NOI18N
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel2MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel2MouseExited(evt);
            }
        });
        getContentPane().add(jLabel2);
        jLabel2.setBounds(140, 30, 70, 70);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/face_1.jpg"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel3MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel3MouseExited(evt);
            }
        });
        getContentPane().add(jLabel3);
        jLabel3.setBounds(1080, 20, 70, 60);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/print1.jpg"))); // NOI18N
        jLabel4.setText("jLabel4");
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel4MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel4MouseExited(evt);
            }
        });
        getContentPane().add(jLabel4);
        jLabel4.setBounds(940, 20, 70, 60);

        jLabel7.setFont(new java.awt.Font("Gabriola", 3, 48));
        jLabel7.setForeground(new java.awt.Color(255, 0, 51));
        jLabel7.setText("Ingredients");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(870, 270, 210, 40);

        ingredients.setBorder(null);
        ingredients.setFont(new java.awt.Font("Franklin Gothic Medium", 0, 18));
        jScrollPane1.setViewportView(ingredients);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(810, 310, 340, 460);
        getContentPane().add(dureeField);
        dureeField.setBounds(590, 350, 140, 30);
        getContentPane().add(note);
        note.setBounds(590, 390, 140, 30);
        getContentPane().add(remarquesField);
        remarquesField.setBounds(550, 490, 190, 120);
        getContentPane().add(origine);
        origine.setBounds(590, 440, 140, 30);

        cout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coutActionPerformed(evt);
            }
        });
        getContentPane().add(cout);
        cout.setBounds(590, 310, 140, 30);

        nbr_personnes.setDisabledTextColor(new java.awt.Color(153, 153, 153));
        nbr_personnes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nbr_personnesActionPerformed(evt);
            }
        });
        getContentPane().add(nbr_personnes);
        nbr_personnes.setBounds(590, 270, 140, 30);
        getContentPane().add(path);
        path.setBounds(480, 650, 160, 20);

        browsbout.setText("Image");
        browsbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browsboutActionPerformed(evt);
            }
        });
        getContentPane().add(browsbout);
        browsbout.setBounds(340, 650, 110, 23);

        jLabel5.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel5.setText("Precedent");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(30, 90, 80, 31);

        jLabel19.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel19.setText("Acceuil");
        getContentPane().add(jLabel19);
        jLabel19.setBounds(150, 100, 60, 14);

        jLabel8.setFont(new java.awt.Font("Gabriola", 0, 24));
        jLabel8.setText("Nombre de Personnes");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(390, 260, 180, 41);

        jLabel10.setFont(new java.awt.Font("Gabriola", 0, 24));
        jLabel10.setText("Cout");
        getContentPane().add(jLabel10);
        jLabel10.setBounds(390, 300, 70, 41);

        jLabel20.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel20.setText("Partager la recette");
        getContentPane().add(jLabel20);
        jLabel20.setBounds(1060, 80, 140, 31);

        jLabel21.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel21.setText("Imprimer");
        getContentPane().add(jLabel21);
        jLabel21.setBounds(950, 80, 140, 31);

        jLabel11.setFont(new java.awt.Font("Gabriola", 0, 24));
        jLabel11.setText("Duree");
        getContentPane().add(jLabel11);
        jLabel11.setBounds(390, 340, 70, 41);

        jLabel17.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ok2.jpg"))); // NOI18N
        jLabel17.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel17MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel17MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel17MouseExited(evt);
            }
        });
        getContentPane().add(jLabel17);
        jLabel17.setBounds(350, 680, 120, 100);

        jLabel12.setFont(new java.awt.Font("Gabriola", 0, 24));
        jLabel12.setText("Note");
        getContentPane().add(jLabel12);
        jLabel12.setBounds(390, 380, 60, 41);

        jLabel13.setFont(new java.awt.Font("Gabriola", 0, 24));
        jLabel13.setText("Remarques");
        getContentPane().add(jLabel13);
        jLabel13.setBounds(370, 490, 100, 41);

        jLabel14.setFont(new java.awt.Font("Gabriola", 0, 24));
        jLabel14.setText("Origine");
        getContentPane().add(jLabel14);
        jLabel14.setBounds(380, 430, 80, 41);

        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret1.jpg"))); // NOI18N
        jLabel16.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel16MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel16MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel16MouseExited(evt);
            }
        });
        getContentPane().add(jLabel16);
        jLabel16.setBounds(30, 40, 90, 50);

        jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/likeoff.jpg"))); // NOI18N
        jLabel18.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel18MouseClicked(evt);
            }
        });
        getContentPane().add(jLabel18);
        jLabel18.setBounds(340, 550, 130, 90);

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/cadr2.jpg"))); // NOI18N
        getContentPane().add(jLabel9);
        jLabel9.setBounds(290, 200, 500, 550);

        jLabel15.setFont(new java.awt.Font("Gabriola", 3, 36));
        jLabel15.setForeground(new java.awt.Color(255, 51, 51));
        jLabel15.setText("Modification");
        getContentPane().add(jLabel15);
        jLabel15.setBounds(510, 20, 230, 60);

        titre.setEditable(false);
        titre.setFont(new java.awt.Font("Gabriola", 3, 36));
        getContentPane().add(titre);
        titre.setBounds(400, 140, 460, 40);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/pin2.jpg"))); // NOI18N
        getContentPane().add(jLabel6);
        jLabel6.setBounds(730, 180, 480, 630);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/cadr2_1.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, -10, 1210, 850);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseEntered
       jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/home2.jpg")));
    }//GEN-LAST:event_jLabel2MouseEntered

    private void jLabel2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseExited
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/home.jpg")));
    }//GEN-LAST:event_jLabel2MouseExited

    private void nbr_personnesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nbr_personnesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nbr_personnesActionPerformed

    private void coutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coutActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_coutActionPerformed

    private void jLabel3MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseEntered
       jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/face2.jpg")));
    }//GEN-LAST:event_jLabel3MouseEntered

    private void jLabel3MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseExited
       jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/face_1.jpg")));
    }//GEN-LAST:event_jLabel3MouseExited

    private void jLabel4MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseEntered
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/print2.jpg")));
    }//GEN-LAST:event_jLabel4MouseEntered

    private void jLabel4MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseExited
       jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/print1.jpg")));
    }//GEN-LAST:event_jLabel4MouseExited

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseClicked
        Menu fa = new Menu();
           fa.setVisible(true);
           this.setVisible(false);
    }//GEN-LAST:event_jLabel2MouseClicked

    private void jLabel16MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseClicked

        this.setVisible(false);
}//GEN-LAST:event_jLabel16MouseClicked

    private void jLabel16MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseEntered
        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret2.jpg")));
}//GEN-LAST:event_jLabel16MouseEntered

    private void jLabel16MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseExited
        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret1.jpg")));
}//GEN-LAST:event_jLabel16MouseExited

    private void jLabel17MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel17MouseClicked
        

        Recette r = new Recette(Recette.idinc,titre.getText(),categorie,ingredients.getText() ,Integer.parseInt(dureeField.getText()),new Float(note.getText()),fav,remarquesField.getText(),person_image,new Float(cout.getText()),Integer.parseInt(nbr_personnes.getText()),origine.getText());
        recetteDAO.update(r);
        JOptionPane.showMessageDialog(this,"Recette modifiée avec succés", "Ok", JOptionPane.INFORMATION_MESSAGE);
        this.hide();
}//GEN-LAST:event_jLabel17MouseClicked

    private void jLabel17MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel17MouseEntered
        jLabel17.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ok1.jpg")));
}//GEN-LAST:event_jLabel17MouseEntered

    private void jLabel17MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel17MouseExited
        jLabel17.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ok2.jpg")));
}//GEN-LAST:event_jLabel17MouseExited

    private void jLabel18MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel18MouseClicked


        switch (fav){
            case 0 :{
                fav=1;
                jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/like.jpg")));
            }
            break;
            case 1 :{
                fav=0;
                jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/likeoff.jpg")));
            }
            break;
        }
    }//GEN-LAST:event_jLabel18MouseClicked

    private void browsboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browsboutActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        String filename = f.getAbsolutePath();

        path.setText(filename);
        try {
            File image = new File(filename);
            FileInputStream fis = new FileInputStream(image);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);

            }
            person_image = bos.toByteArray();
          
        } catch (Exception e) {
        }
}//GEN-LAST:event_browsboutActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               // new RecetteIngredients().setVisible(true);
            }
        });
    }
        int s=0;
    byte[] person_image=null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browsbout;
    private javax.swing.JTextField cout;
    private javax.swing.JTextField dureeField;
    private javax.swing.JTextPane ingredients;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nbr_personnes;
    private javax.swing.JTextField note;
    private javax.swing.JTextField origine;
    private javax.swing.JTextField path;
    private java.util.List<edu.app.persistence.Recette> recettes;
    private javax.swing.JTextField remarquesField;
    private javax.swing.JTextField titre;
    // End of variables declaration//GEN-END:variables
}
